// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

// kthread - An M:N threading library to make applications more concurrent.

// Date: Wed Mar 14 17:44:58 CST 2018

#include <kthread/internal/sys_futex.h>
#include <atomic>
#include <pthread.h>
#include <unordered_map>
#include <mutex>
#include <condition_variable>
#include <turbo/log/logging.h>

#if defined(OS_MACOSX)

namespace kthread {

class SimuFutex {
public:
    SimuFutex() : counts(0)
                , ref(0) {
    }
    ~SimuFutex() {
    }

public:
    std::mutex lock;
    std::condition_variable cond;
    int32_t counts;
    int32_t ref;
};

static std::mutex s_futex_map_mutex;
static std::once_flag init_futex_map_once;
static std::unordered_map<void*, SimuFutex>* s_futex_map = nullptr;
static void InitFutexMap() {
    // Leave memory to process's clean up.
    s_futex_map = new (std::nothrow) std::unordered_map<void*, SimuFutex>();
    if (nullptr == s_futex_map) {
        exit(1);
    }
    return;
}

int futex_wait_private(void* addr1, int expected, const timespec* timeout) {
    std::call_once(init_futex_map_once, InitFutexMap);
    std::unique_lock mu(s_futex_map_mutex);
    SimuFutex& simu_futex = (*s_futex_map)[addr1];
    ++simu_futex.ref;
    mu.unlock();

    int rc = 0;
    {
        std::unique_lock mu1(simu_futex.lock);
        if (static_cast<std::atomic<int>*>(addr1)->load() == expected) {
            ++simu_futex.counts;
            if (timeout) {
                simu_futex.cond.wait_for(mu1,turbo::Duration::to_chrono_nanoseconds(turbo::Duration::from_timespec(*timeout)));
            } else {
                simu_futex.cond.wait(mu1);
            }
            --simu_futex.counts;
        } else {
            errno = EAGAIN;
            rc = -1;
        }
    }

    std::unique_lock mu1(s_futex_map_mutex);
    if (--simu_futex.ref == 0) {
        s_futex_map->erase(addr1);
    }
    mu1.unlock();
    return rc;
}

int futex_wake_private(void* addr1, int nwake) {
    std::call_once(init_futex_map_once, InitFutexMap);
    std::unique_lock mu(s_futex_map_mutex);
    auto it = s_futex_map->find(addr1);
    if (it == s_futex_map->end()) {
        mu.unlock();
        return 0;
    }
    SimuFutex& simu_futex = it->second;
    ++simu_futex.ref;
    mu.unlock();

    int nwakedup = 0;
    int rc = 0;
    {
        std::unique_lock mu1(simu_futex.lock);
        nwake = (nwake < simu_futex.counts)? nwake: simu_futex.counts;
        for (int i = 0; i < nwake; ++i) {
            simu_futex.cond.notify_one();
            ++nwakedup;
        }
    }

    std::unique_lock mu2(s_futex_map_mutex);
    if (--simu_futex.ref == 0) {
        s_futex_map->erase(addr1);
    }
    mu2.unlock();
    return nwakedup;
}

} // namespace kthread

#endif
